<?php
namespace App\Admin\Controllers\FileModel;
use App\Http\Controllers\Controller;
use Dcat\Admin\FormStep\Form as StepForm;
use App\Models\InputStepForm;
use App\Models\InformationList;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
use Maatwebsite\Excel\Facades\Excel;
use Dcat\Admin\Grid;
use Illuminate\Support\Facades\DB;
use PHPExcel_IOFactory; 
use PHPExcel;
use Log;
// use Dcat\Admin\Widgets\Form;
class MoveNotesController extends Controller
{
    //电除尘运行记录表
    protected $options = [
        1 => '显示文本框',
        2 => '显示编辑器',
        3 => '显示文件上传',
        4 => '还是显示文本框',
    ];
    // use PreviewCode;
    public function index(Content $content)
    {
        return $content
            // ->body('<div style="margin:5px 0 15px;">'.$this->buildPreviewButton().'</div>')
            ->body($this->form())
            ->header('电除尘运行记录表')
            ->description('分步表单');
    }
    protected function form()
{
    Admin::js('/stepform/js/jquery.smartWizard.min.js');
    Admin::css('/stepform/css/step.css');
    return Form::make(new InputStepForm(), function (Form $form) {
            $form->title('电除尘运行记录表');
            $form->action('MoveNotesstep');
            $form->disableListButton();
            $form->multipleSteps()
                ->remember() 
                ->width('100%')
                ->add('信息录入', function (StepForm $step) use ($form) {
                    $names = [
                        '一电场' => '一电场',
                        '二电场' => '二电场',
                        '三电场' => '三电场',
                        '四电场' => '四电场',
                        '五电场' => '五电场',
                        '六电场' => '六电场',
                        '七电场' => '七电场',
                        '八电场' => '八电场',
                        '九电场' => '九电场',
                        '十电场' => '十电场',
                        '十一电场' => '十一电场',
                        '十二电场' => '十二电场',
                    ];
                    $step->table('0', trans('24点'),function ($table) use ($names){
                        $table->select('dc', '选择电场名称')->options($names)->width(8,2);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('1', trans('1点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('2', trans('2点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('3', trans('3点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('4', trans('4点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('5', trans('5点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('6', trans('6点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('7', trans('7点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('8', trans('8点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('9', trans('9点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('10', trans('10点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('11', trans('11点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('12', trans('12点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('13', trans('13点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('14', trans('14点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('15', trans('15点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('16', trans('16点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('17', trans('17点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('18', trans('18点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('19', trans('19点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('20', trans('20点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('21', trans('21点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('22', trans('22点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);
                    $step->table('23', trans('23点'),function ($table) use ($names){
                        $table->select('dc', '电场')->options($names);
                        $table->text('一电压','一次电压(V)');
                        $table->text('一电流','一次电流(A)');
                        $table->text('二电压','二次电压(KV)');
                        $table->text('二电流','二次电流(A)');
                        $table->text('输出比','输出比');
                    })->width(12,0);

                    // $step->multipleImage('jdzp', trans('进度照片'))->autoUpload()->url('step/files');
                });
        });
    }

    public function step(Request $request){
        $info = $request->all();
        if(empty($info['CURRENT_VALIDATION_STEP'])&&$info['ALL_STEPS']==1){//提交
            unset($info['_file_']);
            unset($info['CURRENT_VALIDATION_STEP']);
            unset($info['ALL_STEPS']);
            unset($info['_token']);
            unset($info['_previous_']);
            //实例化 Excel 类
            $objPHPExcel = new PHPExcel();
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            $objActSheet = $objPHPExcel->getActiveSheet(0);
            // $objPHPExcel->getActiveSheet()->mergeCells('A1:J1');
            $objActSheet->setTitle('电除尘器运行记录表');//设置excel的标题
            $objActSheet->setCellValue('A1','电除尘器运行记录表');
            $baseRow = 4; //数据从N-1行开始往下输出 这里是避免头信息被覆盖
            foreach ( $info as $r => $d ) {
                $j = $baseRow + $r;
                //电场总数
                $sumL = count($d);
                if($r == 0){
                    $title_num = $this->numToExcelLetter(($sumL*5)+2);
                    $objPHPExcel->getActiveSheet()->mergeCells('A1:'.$title_num.'1');
                    $objPHPExcel->getActiveSheet()->setCellValue($title_num.'2','交班记录');
                }
                $d = array_values($d);
                foreach($d as $dk => $dval){
                    if($r == 0){
                        //第一个电场从B2开始 B2-F2
                        if($dk==0){
                            $objPHPExcel->getActiveSheet()->setCellValue('B2',$dval['dc']);
                            $star = 'B2';
                            $end = 'F2';
                            $tstar = 2;
                            $tend = 6;
                        }else{
                            $i= $dk+1;
                            $objPHPExcel->getActiveSheet()->setCellValue($this->numToExcelLetter(($i-1)*5+2).'2',$dval['dc']);
                            $star = $this->numToExcelLetter(($i-1)*5+2).'2';
                            $end = $this->numToExcelLetter($i*5+1).'2';
                            $tstar = ($i-1)*5+2;
                            $tend = $i*5+1;
                        }
                        $objPHPExcel->getActiveSheet()->mergeCells($star.":".$end);
                        $objPHPExcel->getActiveSheet()->setCellValue($this->numToExcelLetter($tstar).'3','一电压');
                        $objPHPExcel->getActiveSheet()->setCellValue($this->numToExcelLetter($tstar+1).'3','一电流');
                        $objPHPExcel->getActiveSheet()->setCellValue($this->numToExcelLetter($tstar+2).'3','二电压');
                        $objPHPExcel->getActiveSheet()->setCellValue($this->numToExcelLetter($tstar+3).'3','二电流');
                        $objPHPExcel->getActiveSheet()->setCellValue($this->numToExcelLetter($tend).'3','输出比');
                    }
                    if($dk == 0){
                        $star = 2;
                        $end = 6;
                    }else{
                        $dk = $dk+1;
                        $star = ($dk-1)*5+2;
                        $end = $dk*5+1;
                    }
                    $objPHPExcel->getActiveSheet()->setCellValue($this->numToExcelLetter($star).$j,$dval['一电压']);
                    $objPHPExcel->getActiveSheet()->setCellValue($this->numToExcelLetter($star+1).$j,$dval['一电流']);
                    $objPHPExcel->getActiveSheet()->setCellValue($this->numToExcelLetter($star+2).$j,$dval['二电压']);
                    $objPHPExcel->getActiveSheet()->setCellValue($this->numToExcelLetter($star+3).$j,$dval['二电流']);
                    $objPHPExcel->getActiveSheet()->setCellValue($this->numToExcelLetter($end).$j,$dval['输出比']);
                }
            }
            $objPHPExcel->getActiveSheet()->setCellValue('A3','时间');
            $objPHPExcel->getActiveSheet()->setCellValue('A4','24');
            $objPHPExcel->getActiveSheet()->setCellValue('A5','1');
            $objPHPExcel->getActiveSheet()->setCellValue('A6','2');
            $objPHPExcel->getActiveSheet()->setCellValue('A7','3');
            $objPHPExcel->getActiveSheet()->setCellValue('A8','4');
            $objPHPExcel->getActiveSheet()->setCellValue('A9','5');
            $objPHPExcel->getActiveSheet()->setCellValue('A10','6');
            $objPHPExcel->getActiveSheet()->setCellValue('A11','7');
            $objPHPExcel->getActiveSheet()->setCellValue('A12','8');
            $objPHPExcel->getActiveSheet()->setCellValue('A13','9');
            $objPHPExcel->getActiveSheet()->setCellValue('A14','10');
            $objPHPExcel->getActiveSheet()->setCellValue('A15','11');
            $objPHPExcel->getActiveSheet()->setCellValue('A16','12');
            $objPHPExcel->getActiveSheet()->setCellValue('A17','13');
            $objPHPExcel->getActiveSheet()->setCellValue('A18','14');
            $objPHPExcel->getActiveSheet()->setCellValue('A19','15');
            $objPHPExcel->getActiveSheet()->setCellValue('A20','16');
            $objPHPExcel->getActiveSheet()->setCellValue('A21','17');
            $objPHPExcel->getActiveSheet()->setCellValue('A22','18');
            $objPHPExcel->getActiveSheet()->setCellValue('A23','19');
            $objPHPExcel->getActiveSheet()->setCellValue('A24','20');
            $objPHPExcel->getActiveSheet()->setCellValue('A25','21');
            $objPHPExcel->getActiveSheet()->setCellValue('A26','22');
            $objPHPExcel->getActiveSheet()->setCellValue('A27','23');
            
            $data['admin_id'] = Admin::user()->id;
            $data['name'] = '电除尘器运行记录表'.date('Y-m-d H:i');
            $data['data'] = $info;
            // $data['content'] = json_encode($info);
            // $insertData['content'] = json_encode($data);
            $insertData['name'] = $data['name'];
            $insertData['type'] = 11;
            $insertData['admin_id'] = Admin::user()->id;
            $insertData['created_at'] = date('Y-m-d H:i:s');
            // $templateProcessor->saveAs(public_path('filemodellist/'.$info['projectname'].md5(time()) . ".docx"));
            $objWriter->save(public_path('filemodellist/'.$data['name'].".xlsx"));
            InformationList::insert($insertData);
            return $this->form()->saving(function (Form $form) {
                // 清空缓存
                $form->multipleSteps()->flushStash();
                // 拦截保存操作
                return response(
                    $form->multipleSteps()
                        ->done()
                        ->render()
                );
            })->store();
        }
    }

    //根据数字转换成excel的字母
    private function numToExcelLetter($num)
    {
        //由于大写字母只有26个，所以基数为26
        $base = 26;
        $result = '';
 
        while ($num > 0 ) {
            $mod = (int)($num % $base);
            $num = (int)($num / $base);
 
            if($mod == 0){
                $num -= 1;
                $temp = $this->numToLetter($base) . $result;
            } elseif ($num == 0) {
                $temp = $this->numToLetter($mod) . $result;
            } else {
                $temp = $this->numToLetter($mod) . $result;
            }
            $result = $temp;
        }
 
        return $result;
    }
     
    private function numToLetter($num)
    {
        if ($num == 0) {
            return '';
        }
 
        $num = (int)$num - 1;
        //获取A的ascii码
        $ordA = ord('A');
        return chr($ordA + $num);
    }

    /**
     * 保存
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function store(Request $requset)
    {
        return $this->form()->saving(function (Form $form) {
            
            // 清空缓存
            $form->multipleSteps()->flushStash();
            // 拦截保存操作
            return response(
                $form->multipleSteps()
                    ->done()
                    ->render()
            );
        })->store();
    }

    public function list($id,Content $content){
        return $content
            ->header('表格列表')
            ->description()
            ->body($this->grid($id));
    }
    protected function grid($typeid)
    {
        return new Grid(InformationList::where(['admin_id'=>Admin::user()->id,'type'=>$typeid]), function (Grid $grid) {
            $grid->showActions();
            $grid->disableDeleteButton();
            $grid->disableQuickEditButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->fixColumns(2);
            
            $grid->column('id', 'ID');
            $grid->column('name', '名称');
            $grid->column('type','类型')->display(function($type) {
                return Db::table('xfy_info_model')->where(['id'=>$type])->value('name');;
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $id = $actions->getKey();
                $edit_url = admin_base_path('downloadfile/'.$id);
                $actions->append("<a href='$edit_url'>下载</a>");
            });
        });
    }
}